package com.qys.livesMall.anchor.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.anchor.constants.enums.AnchorSellTaskStatus;
import com.qys.livesMall.anchor.mapper.H2AnchorMapper;
import com.qys.livesMall.anchor.model.dto.task.app.SellTaskAnchorPageDTO;
import com.qys.livesMall.anchor.model.dto.task.app.SellTaskHallPageDTO;
import com.qys.livesMall.anchor.model.entity.AnchorLivesSession;
import com.qys.livesMall.anchor.model.vo.task.app.AppSellTaskAnchorPageVO;
import com.qys.livesMall.anchor.model.vo.task.app.SellTaskHallPageVO;
import com.qys.livesMall.anchor.service.AnchorLivesSessionService;
import com.qys.livesMall.anchor.service.AnchorSellTaskService;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.EnumListUtil;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.shop.constants.ShopSellTaskStatus;
import com.qys.livesMall.shop.modal.vo.ShopLevelVO;
import com.qys.livesMall.shop.service.H2ShopLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 主播接取带货任务表 前端控制器
 * </p>
 *
 * @author
 * @since 2023-06-16
 */
@RestController
@Tag(name = "APP-带货主播的任务")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/app/anchor/sell/task")
public class AppAnchorSellTaskController {

    private final AnchorSellTaskService anchorSellTaskService;
    private final H2ShopLevelService shopLevelService;
    private final H2AnchorMapper anchorMapper;
    private final AnchorLivesSessionService livesSessionService;

    @PostMapping("/taskHallPage")
    @Operation(summary = "任务大厅")
    public R<Page<SellTaskHallPageVO>> taskHallPage(@RequestBody SellTaskHallPageDTO queryDTO) {
        queryDTO.setStatus(ShopSellTaskStatus.进行中.getStatus());
        return R.ok(anchorSellTaskService.taskHallPage(queryDTO));
    }


    @GetMapping("/receiveShopSellTask/{sellTaskId}")
    @Operation(summary = "主播接单带货任务", parameters = {@Parameter(name = "sellTaskId", description = "带货任务id")})
    public R<Void> receiveShopSellTask(@PathVariable Long sellTaskId) {
        anchorSellTaskService.receiveShopSellTask(sellTaskId);
        return R.ok();
    }

    @PostMapping("/anchorSellTaskList")
    @Operation(summary = "分页查询主播接单带货任务")
    public R<Page<AppSellTaskAnchorPageVO>> anchorSellTaskList(@RequestBody SellTaskAnchorPageDTO queryDTO) {
        Long anchorId = anchorMapper.queryAnchorIdByUserId(SecurityUtils.getUserId());
        queryDTO.setAnchorId(anchorId);
        return R.ok(anchorSellTaskService.anchorSellTaskList(queryDTO));
    }


    @Operation(summary = "店铺等级列表")
    @GetMapping(value = "/all")
    public R<List<ShopLevelVO>> all() {
        return R.ok(shopLevelService.list());
    }


    @GetMapping("/statusList")
    @Operation(summary = "主播带货任务状态集合")
    public R<List<Map<String, Object>>> statusList() {
        return R.ok(EnumListUtil.enumToListMap(AnchorSellTaskStatus.class));
    }

    @PostMapping("/settlementTask/{id}")
    @Operation(summary = "主播申请结算任务")
    public R<?> settlementTask(@PathVariable Long id) {
        AnchorLivesSession one = livesSessionService.getOne(Wrappers.<AnchorLivesSession>lambdaQuery().eq(AnchorLivesSession::getLivesStatus, (byte) 3)
                .eq(AnchorLivesSession::getAnchorSellTaskId, id).orderByDesc(AnchorLivesSession::getEndTime).last("limit 1"));
        if(one != null && one.getPlatformSettlement() == null){
            one.setPlatformSettlement(0);
            livesSessionService.updateById(one);
            return R.ok();
        }
        return R.fail("Settlement completed");
    }

}
